package com.canaan.business.service.basic.impl;

import java.util.Date;
import java.util.List;

import com.canaan.business.domain.basic.StorehouseAreaShipper;
import com.canaan.business.mapper.basic.StorehouseAreaShipperMapper;
import com.canaan.business.service.basic.IStorehouseAreaShipperService;
import com.canaan.common.utils.DateUtils;
import com.canaan.common.utils.SecurityUtils;
import com.canaan.common.utils.TableNoUtil;
import com.canaan.common.utils.StringUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
/**
 * 库区码货员Service业务层处理
 *
 * @author ruoyi
 * @date 2023-07-06
 */
@Service
public class StorehouseAreaShipperServiceImpl implements IStorehouseAreaShipperService {
    @Resource
    private StorehouseAreaShipperMapper storehouseAreaShipperMapper;

    /**
     * 查询库区码货员
     *
     * @param id 库区码货员主键
     * @return 库区码货员
     */
    @Override
    public StorehouseAreaShipper selectStorehouseAreaShipperById(Long id) {
        return storehouseAreaShipperMapper.selectStorehouseAreaShipperById(id);
    }

    /**
     * 查询库区码货员列表
     *
     * @param storehouseAreaShipper 库区码货员
     * @return 库区码货员
     */
    @Override
    public List<StorehouseAreaShipper> selectStorehouseAreaShipperList(StorehouseAreaShipper storehouseAreaShipper) {
        return storehouseAreaShipperMapper.selectStorehouseAreaShipperList(storehouseAreaShipper);
    }

    @Override
    public List<StorehouseAreaShipper> selectStorehouseAreaShipperListByAreaNo(String areaNo) {
        StorehouseAreaShipper param = new StorehouseAreaShipper();
        param.setStorehouseArea(areaNo);
        return storehouseAreaShipperMapper.selectStorehouseAreaShipperList(param);
    }

    /**
     * 新增库区码货员
     *
     * @param storehouseAreaShipper 库区码货员
     * @return 结果
     */
    @Override
    public int insertStorehouseAreaShipper(StorehouseAreaShipper storehouseAreaShipper) {
        if(StringUtils.isBlank(storehouseAreaShipper.getNo())){
            storehouseAreaShipper.setNo(TableNoUtil.getNo(StorehouseAreaShipper.class));
        }
        storehouseAreaShipper.setCreateBy(SecurityUtils.getUsername());
        storehouseAreaShipper.setCreateTime(DateUtils.getNowDate());
        return storehouseAreaShipperMapper.insertStorehouseAreaShipper(storehouseAreaShipper);
    }

    /**
     * 批量新增库区码货员
     *
     * @param storehouseAreaShipperList 库区码货员列表
     * @return 结果
     */
    @Override
    public int batchInsertStorehouseAreaShipper(List<StorehouseAreaShipper> storehouseAreaShipperList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (StorehouseAreaShipper storehouseAreaShipper : storehouseAreaShipperList){
            if(StringUtils.isBlank(storehouseAreaShipper.getNo())){
                storehouseAreaShipper.setNo(TableNoUtil.getNo(StorehouseAreaShipper.class));
            }
            storehouseAreaShipper.setCreateBy(username);
            storehouseAreaShipper.setCreateTime(currerTime);
        }
        int insertNum = 0;
        for (int i=0; i<storehouseAreaShipperList.size();) {
            int endIndex = i+500;
            if (endIndex > storehouseAreaShipperList.size()) {
                endIndex = storehouseAreaShipperList.size();
            }
            insertNum = insertNum + storehouseAreaShipperMapper.batchInsertStorehouseAreaShipper(storehouseAreaShipperList.subList(i, endIndex));
            i = endIndex;
        }
        return insertNum;
    }

    /**
     * 修改库区码货员
     *
     * @param storehouseAreaShipper 库区码货员
     * @return 结果
     */
    @Override
    public int updateStorehouseAreaShipper(StorehouseAreaShipper storehouseAreaShipper) {
        storehouseAreaShipper.setUpdateBy(SecurityUtils.getUsername());
        storehouseAreaShipper.setUpdateTime(DateUtils.getNowDate());
        return storehouseAreaShipperMapper.updateStorehouseAreaShipper(storehouseAreaShipper);
    }

    /**
     * 批量修改库区码货员
     *
     * @param storehouseAreaShipperList 库区码货员列表
     * @return 结果
     */
    @Override
    public int batchUpdateStorehouseAreaShipper(List<StorehouseAreaShipper> storehouseAreaShipperList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (StorehouseAreaShipper storehouseAreaShipper : storehouseAreaShipperList){
            storehouseAreaShipper.setUpdateBy(username);
            storehouseAreaShipper.setUpdateTime(currerTime);
        }
        int updateNum = 0;
        for (int i=0; i<storehouseAreaShipperList.size();) {
            int endIndex = i+500;
            if (endIndex > storehouseAreaShipperList.size()) {
                endIndex = storehouseAreaShipperList.size();
            }
            updateNum = updateNum + storehouseAreaShipperMapper.batchUpdateStorehouseAreaShipper(storehouseAreaShipperList.subList(i, endIndex));
            i = endIndex;
        }
        return updateNum;
    }

    /**
     * 批量删除库区码货员
     *
     * @param ids 需要删除的库区码货员主键集合
     * @return 结果
     */
    @Override
    public int deleteStorehouseAreaShipperByIds(Long[] ids) {
        return storehouseAreaShipperMapper.deleteStorehouseAreaShipperByIds(ids);
    }

    /**
     * 删除库区码货员信息
     *
     * @param id 库区码货员主键
     * @return 结果
     */
    @Override
    public int deleteStorehouseAreaShipperById(Long id) {
        return storehouseAreaShipperMapper.deleteStorehouseAreaShipperById(id);
    }

    /**
     * 查询库区码货员
     *
     * @param no 编号
     * @return 库区码货员
     */
    @Override
    public StorehouseAreaShipper selectStorehouseAreaShipperByNo(String no) {
        return storehouseAreaShipperMapper.selectStorehouseAreaShipperByNo(no);
    }

    /**
     * 批量删除库区码货员
     *
     * @param nos 需要删除的编号集合
     * @return 结果
     */
    @Override
    public int deleteStorehouseAreaShipperByNos(String[] nos) {
        return storehouseAreaShipperMapper.deleteStorehouseAreaShipperByNos(nos);
    }

    /**
     * 根据库区编号批量删除库区码货员
     *
     * @param areaNo 需要删除的库区编号
     * @return 结果
     */
    @Override
    public int deleteStorehouseAreaShipperByAreaNo(String areaNo){
        return storehouseAreaShipperMapper.deleteStorehouseAreaShipperByAreaNo(areaNo);
    }

    /**
     * 删除库区码货员信息
     *
     * @param no 编号
     * @return 结果
     */
    @Override
    public int deleteStorehouseAreaShipperByNo(String no) {
        return storehouseAreaShipperMapper.deleteStorehouseAreaShipperByNo(no);
    }
}
